package com.example.forum2.Service.impl;

import com.example.forum2.Service.IArtReplyService;
import com.example.forum2.Service.IArticleService;
import com.example.forum2.comment.AppResult;
import com.example.forum2.comment.ResultCode;
import com.example.forum2.dao.ArticleReplyMapper;
import com.example.forum2.exception.ApplicationExeception;
import com.example.forum2.model.ArticleReply;
import com.example.forum2.utils.StringUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class IArtReplyImpl implements IArtReplyService {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private ArticleReplyMapper articleReplyMapper;
    @Override
    public void create(ArticleReply articleReply) {
         if(articleReply==null||articleReply.getArticleId()==null
         ||articleReply.getPostUserId()==null||
                 StringUtil.isEmpty(articleReply.getContent())){
             throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
         }
         articleReply.setReplyId(null);
         articleReply.setReplyUserId(null);
         articleReply.setLikeCount(0);
         articleReply.setState((byte) 0);
         articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
      int row =  articleReplyMapper.insertSelective(articleReply);
      if(row!=1){
          throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED));
      }
      articleService.addOneReplyCountById(articleReply.getArticleId());
    }

    @Override
    public List<ArticleReply> selectByArticledId(Long articleId) {
        if(articleId==null||articleId<=0){
            throw new ApplicationExeception(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        return result;
    }
}
